package com.kla.power.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.entity.MerchantEntity;
import com.kla.power.mapper.MerchantMapper;
import com.kla.power.service.IMerchantService;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商户/部门表 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2023-06-19
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, MerchantEntity> implements IMerchantService {

    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public List<MerchantEntity> queryList ( MerchantEntity merchantEntity ) {
        // 查询自己，以及下属所有分公司
        if ( merchantEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( merchantEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        List<MerchantEntity> merchantEntities = merchantMapper.queryList ( merchantEntity );
        return merchantEntities;
    }

    /**
     * 当前登录账号获取自己所在公司，以及下属所有子公司/部门
     * @param merchantEntity
     * @return
     */
    @Override
    public MerchantEntity querySingle ( MerchantEntity merchantEntity ) {
        if ( merchantEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( merchantEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        MerchantEntity merchant = baseMapper.selectById ( merchantEntity );
        return merchant;
    }


    @Override
    public MerchantEntity saveSingle ( MerchantEntity merchantEntity ) {
        if ( merchantEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( merchantEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        merchantEntity.setCreateTime ( new Date () );
        int insert = baseMapper.insert ( merchantEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return merchantEntity;
    }

    @Override
    public MerchantEntity updateSingle ( MerchantEntity merchantEntity ) {
        if ( merchantEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( merchantEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        UpdateWrapper<MerchantEntity> updateWrapper = new UpdateWrapper<> ();
        if ( StringUtils.isNotBlank ( merchantEntity.getTelPhone () ) ) {
            updateWrapper.set ( "telPhone", merchantEntity.getTelPhone () );
        }
        if ( StringUtils.isNotBlank ( merchantEntity.getMerchantName () ) ) {
            updateWrapper.set ( "merchantName", merchantEntity.getMerchantName () );
        }
        if ( StringUtils.isNotBlank ( merchantEntity.getAddress () ) ) {
            updateWrapper.set ( "address", merchantEntity.getAddress () );
        }
        if ( StringUtils.isNotBlank ( merchantEntity.getParentId () ) ) {
            updateWrapper.set ( "parentId", merchantEntity.getParentId () );
        }
        if ( StringUtils.isNotBlank ( merchantEntity.getLogo () ) ) {
            updateWrapper.set ( "logo", merchantEntity.getLogo () );
        }
        if ( merchantEntity.getEmailNumber () != null ) {
            updateWrapper.set ( "emailNumber", merchantEntity.getEmailNumber () );
        }
        if ( merchantEntity.getContact () != null ) {
            updateWrapper.set ( "contact", merchantEntity.getContact () );
        }
        if ( merchantEntity.getMessageNumber () != null ) {
            updateWrapper.set ( "messageNumber", merchantEntity.getMessageNumber () );
        }
        if ( merchantEntity.getIsUsed () != null ) {
            updateWrapper.set ( "isUsed", merchantEntity.getIsUsed () );
        }
        merchantEntity.setChangeTime ( new Date () );
        updateWrapper.eq ( "id", merchantEntity.getId () );
        int update = baseMapper.update ( null, updateWrapper );
        if ( update == 0 ) {
            throw new StateException ( StateEnum.UPDATE_EXCE );
        }
        return merchantEntity;
    }

}
